package com.breeze.media.video;

import android.content.res.AssetFileDescriptor;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.breeze.media.R;

public class TextureViewActivity extends AppCompatActivity {
    private static final String TAG = "TextureViewActivity";
    private TextureView textureView;
    private MediaPlayer mMediaPlayer;
    private MediaPlayerListener mediaPlayerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texture_view);
        textureView = findViewById(R.id.texture_view);
        initPlayer();
        initTexture();
    }

    private void initTexture() {
        textureView.setSurfaceTextureListener(surfaceTextureListener);
    }

    private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            Log.i(TAG, "onSurfaceTextureAvailable: ");
            if (mMediaPlayer == null) {
                return;
            }
            mMediaPlayer.setSurface(new Surface(surface));
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            Log.i(TAG, "onSurfaceTextureSizeChanged: ");
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            Log.i(TAG, "onSurfaceTextureDestroyed: ");
            if (mMediaPlayer != null) {
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            Log.i(TAG, "onSurfaceTextureUpdated: ");
        }
    };

    private void initPlayer() {
        mMediaPlayer = new MediaPlayer();
        mediaPlayerListener = new MediaPlayerListener();
        mMediaPlayer.setOnCompletionListener(mediaPlayerListener);
        mMediaPlayer.setOnErrorListener(mediaPlayerListener);
        mMediaPlayer.setOnInfoListener(mediaPlayerListener);
        mMediaPlayer.setOnPreparedListener(mediaPlayerListener);
        mMediaPlayer.setOnSeekCompleteListener(mediaPlayerListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mediaPlayerListener);
        try {
            AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.test720);
            mMediaPlayer.setDataSource(afd);
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void playOrPause(View view) {
        if (mMediaPlayer == null) {
            return;
        }
        if (mMediaPlayer.isPlaying()) {
            Log.i(TAG, "pause: ");
            mMediaPlayer.pause();
        } else {
            Log.i(TAG, "start");
            mMediaPlayer.start();
        }
    }

    class MediaPlayerListener implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnVideoSizeChangedListener {

        @Override
        public void onCompletion(MediaPlayer mp) {

        }

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            return false;
        }

        @Override
        public void onPrepared(MediaPlayer mp) {

        }

        @Override
        public void onSeekComplete(MediaPlayer mp) {

        }

        @Override
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

        }
    }
}